<?php
/* SVN FILE: $Id: checkout.php 222 2009-09-16 20:07:22Z supersmashbrothers $ */
/**
* ibMarket 2
* IP.Board 3.0
* Checkout items before buying
*
* @author $Author: supersmashbrothers $
* @copyright $Copyright$
* @version $Revision: 222 $
* @lastrevision $Date: 2009-09-16 20:07:22 +0000 (Wed, 16 Sep 2009) $
* @modifiedby $LastChangedBy: supersmashbrothers $
* @lastmodified $LastChangedDate: 2009-09-16 20:07:22 +0000 (Wed, 16 Sep 2009) $
* @filesource $URL: http://ipb-ibmarket.googlecode.com/svn/ibmarket2/trunk/admin/applications_addon/other/ibmarket/modules_public/shop/checkout.php $
*/

class public_ibmarket_shop_checkout extends marketPublicCommand
{
	public function doExecute( ipsRegistry $registry )
	{
		if($this->memberData['member_id'] == 0 || !$this->memberData['g_market_use'])
		{
			$this->registry->getClass('output')->showError('no_permission');
		}
		
		$this->request['category'] = is_array($this->caches['market_category'][$this->request['category']]) ? $this->request['category'] : -1;
		$this->request['sub'] = isset($this->request['sub']) ? intval($this->request['sub']) : ($this->request['category'] == -1 ? 1 : 0);
		
		$inventoryByID = array();
		foreach(marketRegistry::getInventoryItem($this->memberData['member_id']) as $invItem)
		{
			$inventoryByID[$invItem['item']][] = $invItem;
		}
		
		$checkoutItems = array();
		$total = array(
						'quanity' => 0,
						'price' => 0,
						'tax' => 0,
					);
		foreach($this->request['buy'] as $item_id => $quanity)
		{
			if(!is_array($this->caches['market_items'][$item_id]) || $this->caches['market_items'][$item_id]['hide_item'] || in_array($this->memberData['member_group_id'],$this->caches['market_items'][$item_id]['limit_buy']))
			{
				continue;
			}
			$quanity = numbers::parseInt($quanity);
			if(numbers::compare($this->caches['market_items'][$item_id]['stock']) >= 0 && numbers::compare($this->caches['market_items'][$item_id]['stock'],$quanity) < 0)
			{
				$quanity = $this->caches['market_items'][$item_id]['stock'];
			}
			if($this->caches['market_items'][$item_id]['inventory_limit'] && numbers::compare($this->caches['market_items'][$item_id]['inventory_limit'],numbers::add($quanity,count($inventoryByID[$item_id]))) < 0)
			{
				$quanity = numbers::subtract($this->caches['market_items'][$item_id]['inventory_limit'],count($inventoryByID[$item_id]));
			}
			if(numbers::compare($quanity) <= 0)
			{
				continue;
			}
			$item = $this->caches['market_items'][$item_id];
			$item['buy'] = array();
			$item['buy']['quanity'] = $quanity;
			$item['buy']['subtotal'] = numbers::multiply($item['price'],$quanity);
			$item['buy']['totaltax'] = numbers::divide(numbers::multiply($item['total_tax'],$item['buy']['subtotal']),100);
			$checkoutItems[] = $item;
			
			$total['quanity'] = numbers::add($total['quanity'],$item['buy']['quanity']);
			$total['price'] = numbers::add($total['price'],$item['buy']['subtotal']);
			$total['tax'] = numbers::add($total['tax'],$item['buy']['totaltax']);
		}
		$total['total'] = numbers::add($total['price'],$total['tax']);
		
		if(count($checkoutItems) == 0)
		{
			$this->registry->getClass('output')->silentRedirect($this->settings['base_url_with_app']."module=shop&amp;category=".$this->request['category']."&amp;sub=".$this->request['sub']);
		}
		
		if($this->request['category'] == -1)
		{
			$this->registry->getClass('output')->addNavigation( $this->lang->words['view_all_items'], "" );
		}
		else
		{
			$categories = array();
			$category = $this->request['category'];
			while($category != -1)
			{
				array_unshift($categories, $category);
				$category = $this->caches['market_category'][$category]['cat_parent_id'];
			}
			foreach($categories as $category)
			{
				$this->registry->getClass('output')->addNavigation( $this->caches['market_category'][$category]['cat_name'], "app=ibmarket&amp;category=".$category );
			}
		}
		$this->registry->getClass('output')->addNavigation( $this->lang->words['checkout'], "" );
		
		$content = $this->registry->getClass('output')->getTemplate('ibmarket')->checkoutPage($checkoutItems, $total);
		
		$template = $this->registry->getClass('output')->getTemplate('ibmarket')->ibmarketHTMLWrapper($content);
		
		$this->registry->getClass('output')->setTitle( $this->settings['market_name'] );
		$this->registry->getClass('output')->addContent( $template );
		
        $this->registry->getClass('output')->sendOutput();
	}
}